package cn.slipi.admin.biz;

import cn.slipi.admin.biz.vo.consumer.CsmProjectVo;
import cn.slipi.admin.common.dto.PageDTO;
import cn.slipi.admin.common.exception.base.BizException;
import cn.slipi.admin.common.utils.WordUtil;
import cn.slipi.admin.common.utils.dozer.impl.ConvertBean;
import cn.slipi.admin.controller.form.consumer.GrantForm;
import cn.slipi.admin.controller.form.consumer.ListForm;
import cn.slipi.admin.controller.form.consumer.SaveForm;
import cn.slipi.admin.entity.CsmGrant;
import cn.slipi.admin.entity.CsmProject;
import cn.slipi.admin.service.CsmGrantService;
import cn.slipi.admin.service.CsmProjectService;
import cn.slipi.admin.service.PdrApiService;
import cn.slipi.admin.service.dto.in.consumer.GrantDTO;
import cn.slipi.admin.service.dto.in.consumer.ListDTO;
import cn.slipi.admin.service.dto.in.consumer.SaveDTO;
import cn.slipi.admin.service.dto.out.doc.WriteDTO;
import cn.slipi.admin.task.PublishTask;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;


@Component
public class ConsumerBiz {

    @Autowired
    private ConvertBean convertBean;

    @Autowired
    private PublishTask publishTask;
    @Autowired
    private PdrApiService pdrApiService;

    @Autowired
    private CsmGrantService csmGrantService;

    @Autowired
    private CsmProjectService csmProjectService;


    public PageDTO<CsmProjectVo> getList(ListForm listForm) {
        ListDTO listDTO = this.convertBean.convert(listForm, ListDTO.class);
        PageDTO<CsmProject> page = this.csmProjectService.getList(listDTO);
        return PageDTO.of(page, csmProject -> this.convertBean.convert(csmProject, CsmProjectVo.class));
    }

    public Integer add(SaveForm saveForm) {
        SaveDTO saveDTO = this.convertBean.convert(saveForm, SaveDTO.class);
        return this.csmProjectService.add(saveDTO);
    }

    public Boolean unGrant(Integer grantId) {
        CsmGrant csmGrant = this.csmProjectService.unGrant(grantId);
        this.publishTask.unPublishCsmGrant(csmGrant);
        return true;
    }

    public Boolean grantPublish(Integer grantId) {
        CsmGrant csmGrant = this.csmGrantService.getGrantById(grantId);
        if (csmGrant == null) {
            throw new BizException(10000, "record.found.not");
        }
        List<CsmGrant> csmGrantList = new ArrayList<>();
        csmGrantList.add(csmGrant);
        this.publishTask.publishCsmGrant(csmGrantList);
        return true;
    }

    public Boolean grant(GrantForm grantForm) {
        GrantDTO grantDTO = this.convertBean.convert(grantForm, GrantDTO.class);
        List<CsmGrant> csmGrantList = this.csmProjectService.grant(grantDTO);
        this.publishTask.publishCsmGrant(csmGrantList);
        return true;
    }

    public Boolean update(Integer id, SaveForm saveForm) {
        SaveDTO saveDTO = this.convertBean.convert(saveForm, SaveDTO.class);
        List<CsmGrant> csmGrantList = this.csmProjectService.update(id, saveDTO);
        if (csmGrantList.size() > 0) {
            this.publishTask.publishCsmGrant(csmGrantList);
        }
        return true;
    }

    public WriteDTO write(Integer id) {
        return this.pdrApiService.makeApiDoc(id);
    }

}
